<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      size="small"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="仓库名" prop="name">
        <el-input
          v-model="dataForm.name"
          clearable
          placeholder="仓库名"
        ></el-input>
      </el-form-item>
      <el-form-item label="仓库地址" prop="address">
        <el-input
          v-model="dataForm.address"
          clearable
          placeholder="仓库地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="区域编码" prop="areaCode">
        <el-input
          v-model="dataForm.areaCode"
          clearable
          placeholder="区域编码"
        ></el-input>
      </el-form-item>
      <el-form-item label="仓库状态" size="mini">
        <el-radio-group v-model="dataForm.dataState">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { wareInfoSaveAndUpdate } from "@/api/ware";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        address: "",
        areaCode: "",
        dataState: 0,
      },
      dataRule: {
        name: [{ required: true, message: "仓库名不能为空", trigger: "blur" }],
        address: [
          { required: true, message: "仓库地址不能为空", trigger: "blur" },
        ],
        areaCode: [
          { required: true, message: "区域编码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(data) {
      this.dataForm.id = 0;

      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (data) {
          this.dataForm.id = data.id;
          this.dataForm.name = data.name;
          this.dataForm.address = data.address;
          this.dataForm.areaCode = data.areaCode;
          this.dataForm.dataState = data.dataState;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          wareInfoSaveAndUpdate({
            id: this.dataForm.id || "",
            name: this.dataForm.name,
            address: this.dataForm.address,
            areaCode: this.dataForm.areaCode,
            dataState: this.dataForm.dataState,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
